using MicroMsg.Common.Event;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace MicroMsg.UI.UserContrl
{
	public class ProfileBrandListCtrl : UserControl
	{
		private const string TAG = "ProfileBrandListCtrl";
		private const int cNMaxCacheCnt = 1;
		private bool mBIsFirstLoaded = true;
		public static DependencyProperty ItemObjectProperty = DependencyProperty.Register("ItemObject", typeof(Contact), typeof(ProfileBrandListCtrl), new PropertyMetadata(null, new PropertyChangedCallback(ProfileBrandListCtrl.OnItemObjectPropertyChanged)));
		private EventWatcher mWatchContact;
		private object mObjectWatcher = new object();
		public string mStrNowShown;
		public bool mBShowNotShowCnt;
		private static List<ProfileBrandListCtrl> sItemsCached = new List<ProfileBrandListCtrl>();
		internal StackPanel LayoutRoot;
		internal TextBlock txtAreaTitle;
		internal Button btnContent;
		internal TextBlock txtNotShowCnt;
		internal StackPanel panelHeads;
		private bool _contentLoaded;
		public Contact ItemObject
		{
			get
			{
				return (Contact)base.GetValue(ProfileBrandListCtrl.ItemObjectProperty);
			}
			set
			{
				base.SetValue(ProfileBrandListCtrl.ItemObjectProperty, value);
			}
		}
		public ProfileBrandListCtrl()
		{
			base.set_Margin(new Thickness(0.0, 15.0, 0.0, 0.0));
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.Control_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Control_Unloaded));
		}
		private void Control_Loaded(object sender, RoutedEventArgs e)
		{
		}
		private void Control_Unloaded(object sender, RoutedEventArgs e)
		{
		}
		private static void OnItemObjectPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == ProfileBrandListCtrl.ItemObjectProperty)
			{
				(dependencyObject_0 as ProfileBrandListCtrl).ItemObjectPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void ItemObjectPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_OldValue() != dependencyPropertyChangedEventArgs_0.get_NewValue())
			{
				this.RefreshItem();
			}
		}
		private void registerEvent()
		{
			if (this.mWatchContact == null)
			{
				this.mWatchContact = new EventWatcher(this.mObjectWatcher, null, new EventHandlerDelegate(this.OnContactEvent));
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_ACCOUNT_UPDATED, this.mWatchContact);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchContact != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_ACCOUNT_UPDATED, this.mWatchContact);
				this.mWatchContact = null;
			}
		}
		private void OnContactEvent(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			EventConst mEventID = evtArgs.mEventID;
			if (mEventID != EventConst.ON_STORAGE_ACCOUNT_UPDATED)
			{
				return;
			}
			if (this.ItemObject == null)
			{
				return;
			}
			if (AccountMgr.strUsrName == this.ItemObject.strUsrName)
			{
				Account curAccount = AccountMgr.getCurAccount();
				if (curAccount != null)
				{
					this.ItemObject.strMyBrandList = curAccount.MyBrandList;
					this.Refresh(this.ItemObject.strMyBrandList);
				}
			}
		}
		private void btnContent_mClicked(object sender, object args)
		{
			object[] object_ = new object[]
			{
				this.ItemObject
			};
			ToolFunction.GotoPageForUserControl("/Source/UI/Contact/ProfileInfo/BrandListPage.xaml", object_, false);
		}
		public void Refresh(string strToShow)
		{
			if (strToShow == this.mStrNowShown)
			{
				return;
			}
			this.mStrNowShown = strToShow;
			List<BrandUser> list = BrandUser.fromXml(strToShow);
			if (list == null)
			{
				list = new List<BrandUser>();
			}
			int num = 0;
			for (int i = 0; i < list.get_Count(); i++)
			{
				if (list.get_Item(i).nHidden != 1u)
				{
					BrandHeadCtrl brandHeadCtrl = this.panelHeads.get_Children().get_Item(num) as BrandHeadCtrl;
					brandHeadCtrl.set_Height(75.0);
					brandHeadCtrl.set_Width(75.0);
					brandHeadCtrl.ItemObject = list.get_Item(i).ToContact();
					num++;
					if (num == this.panelHeads.get_Children().get_Count())
					{
						break;
					}
				}
			}
			for (int i = num; i < this.panelHeads.get_Children().get_Count(); i++)
			{
				BrandHeadCtrl brandHeadCtrl2 = this.panelHeads.get_Children().get_Item(num) as BrandHeadCtrl;
				if (brandHeadCtrl2 != null)
				{
					brandHeadCtrl2.ItemObject = null;
				}
			}
			if (num != 0)
			{
				this.panelHeads.set_Visibility(0);
				this.txtNotShowCnt.set_Visibility(1);
				base.set_Visibility(0);
				return;
			}
			if (this.mBShowNotShowCnt)
			{
				this.txtNotShowCnt.set_Text(string.Format(strings.Contacts_Brand_NoVisible, list.get_Count()));
				this.txtNotShowCnt.set_Visibility(0);
				this.panelHeads.set_Visibility(1);
				return;
			}
			base.set_Visibility(1);
		}
		private void RefreshItem()
		{
			if (this.ItemObject == null)
			{
				using (IEnumerator<UIElement> enumerator = this.panelHeads.get_Children().GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						UIElement current = enumerator.get_Current();
						BrandHeadCtrl brandHeadCtrl = current as BrandHeadCtrl;
						brandHeadCtrl.ItemObject = null;
					}
				}
				return;
			}
			if (this.ItemObject.strUsrName == AccountMgr.strUsrName)
			{
				Account curAccount = AccountMgr.getCurAccount();
				if (curAccount != null)
				{
					this.ItemObject.strMyBrandList = curAccount.MyBrandList;
				}
			}
			this.txtAreaTitle.set_Text(strings.Contacts_Brand);
			this.Refresh(this.ItemObject.strMyBrandList);
		}
		public static ProfileBrandListCtrl GetOneItem()
		{
			if (ProfileBrandListCtrl.sItemsCached != null && ProfileBrandListCtrl.sItemsCached.get_Count() != 0)
			{
				ProfileBrandListCtrl profileBrandListCtrl = Enumerable.First<ProfileBrandListCtrl>(ProfileBrandListCtrl.sItemsCached);
				ProfileBrandListCtrl.sItemsCached.RemoveAt(0);
				if (profileBrandListCtrl.get_Parent() == null)
				{
					profileBrandListCtrl.registerEvent();
					return profileBrandListCtrl;
				}
			}
			ProfileBrandListCtrl profileBrandListCtrl2 = new ProfileBrandListCtrl();
			profileBrandListCtrl2.registerEvent();
			return profileBrandListCtrl2;
		}
		public static int GetCacheCnt()
		{
			if (ProfileBrandListCtrl.sItemsCached == null)
			{
				return 0;
			}
			return ProfileBrandListCtrl.sItemsCached.get_Count();
		}
		public static void PushOneItem(ProfileBrandListCtrl item)
		{
			if (item == null)
			{
				return;
			}
			if (ProfileBrandListCtrl.sItemsCached == null)
			{
				ProfileBrandListCtrl.sItemsCached = new List<ProfileBrandListCtrl>();
			}
			if (item.get_Parent() != null)
			{
				Panel panel = item.get_Parent() as Panel;
				if (panel != null)
				{
					panel.get_Children().Remove(item);
				}
			}
			item.Collect();
			if (ProfileBrandListCtrl.sItemsCached.get_Count() >= 1)
			{
				return;
			}
			ProfileBrandListCtrl.sItemsCached.Add(item);
		}
		public void Collect()
		{
			this.ItemObject = null;
			this.mStrNowShown = null;
			this.unRegisterEvent();
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Contact/ProfileInfo/ProfileBrandListCtrl.xaml", 2));
			this.LayoutRoot = (StackPanel)base.FindName("LayoutRoot");
			this.txtAreaTitle = (TextBlock)base.FindName("txtAreaTitle");
			this.btnContent = (Button)base.FindName("btnContent");
			this.txtNotShowCnt = (TextBlock)base.FindName("txtNotShowCnt");
			this.panelHeads = (StackPanel)base.FindName("panelHeads");
		}
	}
}
